<?php
class Pageley_Tag_Dispatch extends Pageley_Tag_Abstract {
    
    private $controller;
    private $successView;
    private $context;
    
    public function execute() {
        $factory = Pageley_Config::getInstance();
        $requestDispatcherName = "requestDispatcher";
        if($this->context !== null) {
            $parts = Pageley_Util_String::split($this->context, "-");
            $context = "";
            foreach($parts as $part) {
                if(Pageley_Util_String::length($context) == 0) {
                    $context .= $part;
                } else {
                    $context .= Pageley_Util_String::capitalize($part);
                }
            }
            $requestDispatcherName = $context . "." . $requestDispatcherName;
        }
        $dispatcher = $factory->valueOf($requestDispatcherName);
        $parameters = null;
        if($this->controller !== null) {
            $parameters = new Pageley_Util_ArrayMap(array(
                "controller" => $this->controller
            ));
        }
        if($this->successView !== null) {
            if($parameters === null) {
                $parameters = new Pageley_Util_ArrayMap();
            }
            $parameters->put("successView", $this->successView);
        }
        $this->dispatch($dispatcher, $parameters);
    }
    
    protected function dispatch($dispatcher, $parameters) {
        $dispatcher->dispatchRequest($parameters);
    }
    
    public function setController($controller) {
        $this->controller = $controller;
    }
    
    public function setContext($context) {
        $this->context = $context;
    }
    
    public function setSuccessView($successView) {
        $this->successView = $successView;
    }
    
}